package com.china08.yxyapi.api.home.activity.healthcompetition;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.huodong.healthcompetition.YxyCompetitionStudentAnswer;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.huodong.YxyCompetitionStudentAnswerRepository;

/**
 * 点击下一题时判断当前题目是否已作答
 * 
 * @author zd.Jin
 *
 *
 */
@RestController
@CrossOrigin
public class QuesJudgeAnswerOrNotController extends YxyApiBaseController {
    @Autowired
    private YxyCompetitionStudentAnswerRepository yxyCompetitionStudentAnswerRepository;

    @RequestMapping(value = "/api_yw/home/activity/healthcompetition/quesanswer/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        RespModel respModel = new RespModel();
        YxyCompetitionStudentAnswer answer = yxyCompetitionStudentAnswerRepository
                .findTop1ByStudentIdAndQuesOrder(reqModel.studentId, reqModel.quesOrder);
        if (answer != null) {
            respModel.status = 0;
        } else {
            respModel.status = 1;
        }
        return respModel;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

        public Integer quesOrder;// 题目order
        public String studentId;// 学生id

        public void check() {
            Assert.notNull(StringUtils.defaultIfBlank(studentId, null), "学生id不能为空");
        }

    }

    protected static class RespModel extends YxyApiRespModel {
        // 当前题目是否作答过(0已作答1未作答)
        public Integer status;
    }
}
